define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template', 'form'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template, Form) {

    var Controller = {
        index: function () {
            // 基于准备好的dom，初始化echarts实例
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
            option = {
                tooltip: {
                    formatter: '{a} <br/>{b} : {c}%'
                },
                series: [{
                    name: Config.achievement.name,
                    type: 'gauge',
                    detail: {
                        formatter: '{value}'
                    },
                    data: [{
                        value: Config.achievement.complete_percent,
                        name: '完成率(%)'
                    }]
                }]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
           ;

            var team_echart = Echarts.init(document.getElementById('team_echart'), 'walden');
            option.series[0].data[0].value=Config.team_achievement.complete_percent;
            team_echart.setOption(option);


            $(".row_config").on("change",function () {
                var obj=$(this);
                var idstr =obj.parent().find(".achievement_type").val();
                if (idstr){
                    Controller.api.eventachievement(team_echart,obj);
                }else{
                    Controller.api.eventachievement(myChart,obj);
                }

            });
            $(".row_year,.row_month").on("blur",function () {
                var obj=$(this);
                var idstr =obj.parent().find(".achievement_type").val();
                if (idstr){
                    Controller.api.eventachievement(team_echart,obj);
                }else{
                    Controller.api.eventachievement(myChart,obj);
                }
            });

            require.config({
                    paths: {
                        'Gzyechart': '../addons/facrm/js/gzyechart',
                    }
                }
            );
            require(['Gzyechart'], function () {
                Form.api.bindevent($("form[role=form]"));
                // 基于准备好的dom，初始化健康表格数据
                Gzyechart.echart.loadEchart("achievement", Echartdata.achievement.date, Echartdata.achievement.data);
                Gzyechart.echart.loadEchart("record", Echartdata.record.date, Echartdata.record.data);


                Gzyechart.echart.loadEchart("team_achievement", TeamEchartdata.team_achievement.date, TeamEchartdata.team_achievement.data);
                Gzyechart.echart.loadEchart("team_record", TeamEchartdata.team_record.date, TeamEchartdata.team_record.data);
                Gzyechart.echart.initButton();

                $("#two").removeClass('active');

            });

        },

        api: {
            eventachievement: function (myChart,obj) {
                var data={};
                data["row_config"] =obj.parent().find(".row_config").val();
                var idstr =obj.parent().find(".achievement_type").val();
                if (idstr)idstr=idstr+"_";
                //获取年份
                data["row_year"] =obj.parent().find(".row_year").val();
                data["row_month"] =obj.parent().find(".row_month").val();
                data["ajax_type"] =idstr+'achievement';
                Fast.api.ajax({
                    url: 'facrm/dashboard/index',
                    data: data
                },function (data) {
                    option.series[0].data[0].value=data.complete_percent;
                    option.series[0].name=data.name;
                    myChart.setOption(option);
                    $("#"+idstr+"yeartarget").html(data.yeartarget);
                    $("#"+idstr+"contract_money").html(data.contract_money);
                    $("#"+idstr+"receivables_money").html(data.receivables_money);
                    return false;
                });
            },
        }
    };

    return Controller;
});
